{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({\n",
    "    \"id\": np.arange(10),\n",
    "    \"name\": [\"a\",\"b\"]*5,\n",
    "    \"a\": [f\"a{i}\" for i in range(10)],\n",
    "    \"b\": [f\"b{i}\" for i in range(10)],\n",
    "    \"aa\": [f\"aa{i}\" for i in range(10)],\n",
    "    \"bb\": [f\"bb{i}\" for i in range(10)],\n",
    "    \"aaa\": [f\"aaa{i}\" for i in range(10)],\n",
    "    \"bbb\": [f\"bbb{i}\" for i in range(10)]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>aaa</th>\n",
       "      <th>bbb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>a0</td>\n",
       "      <td>b0</td>\n",
       "      <td>aa0</td>\n",
       "      <td>bb0</td>\n",
       "      <td>aaa0</td>\n",
       "      <td>bbb0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>a1</td>\n",
       "      <td>b1</td>\n",
       "      <td>aa1</td>\n",
       "      <td>bb1</td>\n",
       "      <td>aaa1</td>\n",
       "      <td>bbb1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>a2</td>\n",
       "      <td>b2</td>\n",
       "      <td>aa2</td>\n",
       "      <td>bb2</td>\n",
       "      <td>aaa2</td>\n",
       "      <td>bbb2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>a3</td>\n",
       "      <td>b3</td>\n",
       "      <td>aa3</td>\n",
       "      <td>bb3</td>\n",
       "      <td>aaa3</td>\n",
       "      <td>bbb3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>a4</td>\n",
       "      <td>b4</td>\n",
       "      <td>aa4</td>\n",
       "      <td>bb4</td>\n",
       "      <td>aaa4</td>\n",
       "      <td>bbb4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>a5</td>\n",
       "      <td>b5</td>\n",
       "      <td>aa5</td>\n",
       "      <td>bb5</td>\n",
       "      <td>aaa5</td>\n",
       "      <td>bbb5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>a6</td>\n",
       "      <td>b6</td>\n",
       "      <td>aa6</td>\n",
       "      <td>bb6</td>\n",
       "      <td>aaa6</td>\n",
       "      <td>bbb6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>a7</td>\n",
       "      <td>b7</td>\n",
       "      <td>aa7</td>\n",
       "      <td>bb7</td>\n",
       "      <td>aaa7</td>\n",
       "      <td>bbb7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>a8</td>\n",
       "      <td>b8</td>\n",
       "      <td>aa8</td>\n",
       "      <td>bb8</td>\n",
       "      <td>aaa8</td>\n",
       "      <td>bbb8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>a9</td>\n",
       "      <td>b9</td>\n",
       "      <td>aa9</td>\n",
       "      <td>bb9</td>\n",
       "      <td>aaa9</td>\n",
       "      <td>bbb9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id name   a   b   aa   bb   aaa   bbb\n",
       "0   0    a  a0  b0  aa0  bb0  aaa0  bbb0\n",
       "1   1    b  a1  b1  aa1  bb1  aaa1  bbb1\n",
       "2   2    a  a2  b2  aa2  bb2  aaa2  bbb2\n",
       "3   3    b  a3  b3  aa3  bb3  aaa3  bbb3\n",
       "4   4    a  a4  b4  aa4  bb4  aaa4  bbb4\n",
       "5   5    b  a5  b5  aa5  bb5  aaa5  bbb5\n",
       "6   6    a  a6  b6  aa6  bb6  aaa6  bbb6\n",
       "7   7    b  a7  b7  aa7  bb7  aaa7  bbb7\n",
       "8   8    a  a8  b8  aa8  bb8  aaa8  bbb8\n",
       "9   9    b  a9  b9  aa9  bb9  aaa9  bbb9"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 这是原始的数据\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>aaa</th>\n",
       "      <th>bbb</th>\n",
       "      <th>merge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>a0</td>\n",
       "      <td>b0</td>\n",
       "      <td>aa0</td>\n",
       "      <td>bb0</td>\n",
       "      <td>aaa0</td>\n",
       "      <td>bbb0</td>\n",
       "      <td>a0,b0#aa0,bb0#aaa0,bbb0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>a1</td>\n",
       "      <td>b1</td>\n",
       "      <td>aa1</td>\n",
       "      <td>bb1</td>\n",
       "      <td>aaa1</td>\n",
       "      <td>bbb1</td>\n",
       "      <td>a1,b1#aa1,bb1#aaa1,bbb1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>a2</td>\n",
       "      <td>b2</td>\n",
       "      <td>aa2</td>\n",
       "      <td>bb2</td>\n",
       "      <td>aaa2</td>\n",
       "      <td>bbb2</td>\n",
       "      <td>a2,b2#aa2,bb2#aaa2,bbb2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>a3</td>\n",
       "      <td>b3</td>\n",
       "      <td>aa3</td>\n",
       "      <td>bb3</td>\n",
       "      <td>aaa3</td>\n",
       "      <td>bbb3</td>\n",
       "      <td>a3,b3#aa3,bb3#aaa3,bbb3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>a4</td>\n",
       "      <td>b4</td>\n",
       "      <td>aa4</td>\n",
       "      <td>bb4</td>\n",
       "      <td>aaa4</td>\n",
       "      <td>bbb4</td>\n",
       "      <td>a4,b4#aa4,bb4#aaa4,bbb4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>a5</td>\n",
       "      <td>b5</td>\n",
       "      <td>aa5</td>\n",
       "      <td>bb5</td>\n",
       "      <td>aaa5</td>\n",
       "      <td>bbb5</td>\n",
       "      <td>a5,b5#aa5,bb5#aaa5,bbb5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>a6</td>\n",
       "      <td>b6</td>\n",
       "      <td>aa6</td>\n",
       "      <td>bb6</td>\n",
       "      <td>aaa6</td>\n",
       "      <td>bbb6</td>\n",
       "      <td>a6,b6#aa6,bb6#aaa6,bbb6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>a7</td>\n",
       "      <td>b7</td>\n",
       "      <td>aa7</td>\n",
       "      <td>bb7</td>\n",
       "      <td>aaa7</td>\n",
       "      <td>bbb7</td>\n",
       "      <td>a7,b7#aa7,bb7#aaa7,bbb7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>a8</td>\n",
       "      <td>b8</td>\n",
       "      <td>aa8</td>\n",
       "      <td>bb8</td>\n",
       "      <td>aaa8</td>\n",
       "      <td>bbb8</td>\n",
       "      <td>a8,b8#aa8,bb8#aaa8,bbb8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>a9</td>\n",
       "      <td>b9</td>\n",
       "      <td>aa9</td>\n",
       "      <td>bb9</td>\n",
       "      <td>aaa9</td>\n",
       "      <td>bbb9</td>\n",
       "      <td>a9,b9#aa9,bb9#aaa9,bbb9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id name   a   b   aa   bb   aaa   bbb                    merge\n",
       "0   0    a  a0  b0  aa0  bb0  aaa0  bbb0  a0,b0#aa0,bb0#aaa0,bbb0\n",
       "1   1    b  a1  b1  aa1  bb1  aaa1  bbb1  a1,b1#aa1,bb1#aaa1,bbb1\n",
       "2   2    a  a2  b2  aa2  bb2  aaa2  bbb2  a2,b2#aa2,bb2#aaa2,bbb2\n",
       "3   3    b  a3  b3  aa3  bb3  aaa3  bbb3  a3,b3#aa3,bb3#aaa3,bbb3\n",
       "4   4    a  a4  b4  aa4  bb4  aaa4  bbb4  a4,b4#aa4,bb4#aaa4,bbb4\n",
       "5   5    b  a5  b5  aa5  bb5  aaa5  bbb5  a5,b5#aa5,bb5#aaa5,bbb5\n",
       "6   6    a  a6  b6  aa6  bb6  aaa6  bbb6  a6,b6#aa6,bb6#aaa6,bbb6\n",
       "7   7    b  a7  b7  aa7  bb7  aaa7  bbb7  a7,b7#aa7,bb7#aaa7,bbb7\n",
       "8   8    a  a8  b8  aa8  bb8  aaa8  bbb8  a8,b8#aa8,bb8#aaa8,bbb8\n",
       "9   9    b  a9  b9  aa9  bb9  aaa9  bbb9  a9,b9#aa9,bb9#aaa9,bbb9"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 把要拆分的列，merge到一列\n",
    "def merge_cols(x):\n",
    "    y = x.values\n",
    "    result = []\n",
    "    for idx in range(0, len(y), 2):\n",
    "        result.append(f\"{y[idx]},{y[idx+1]}\")\n",
    "    return \"#\".join(result)\n",
    "        \n",
    "df[\"merge\"] = df.loc[:, \"a\":\"bbb\"].apply(merge_cols, axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>merge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>[a0,b0, aa0,bb0, aaa0,bbb0]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>[a1,b1, aa1,bb1, aaa1,bbb1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>[a2,b2, aa2,bb2, aaa2,bbb2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>[a3,b3, aa3,bb3, aaa3,bbb3]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>[a4,b4, aa4,bb4, aaa4,bbb4]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>[a5,b5, aa5,bb5, aaa5,bbb5]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>[a6,b6, aa6,bb6, aaa6,bbb6]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>[a7,b7, aa7,bb7, aaa7,bbb7]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>[a8,b8, aa8,bb8, aaa8,bbb8]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>[a9,b9, aa9,bb9, aaa9,bbb9]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id name                        merge\n",
       "0   0    a  [a0,b0, aa0,bb0, aaa0,bbb0]\n",
       "1   1    b  [a1,b1, aa1,bb1, aaa1,bbb1]\n",
       "2   2    a  [a2,b2, aa2,bb2, aaa2,bbb2]\n",
       "3   3    b  [a3,b3, aa3,bb3, aaa3,bbb3]\n",
       "4   4    a  [a4,b4, aa4,bb4, aaa4,bbb4]\n",
       "5   5    b  [a5,b5, aa5,bb5, aaa5,bbb5]\n",
       "6   6    a  [a6,b6, aa6,bb6, aaa6,bbb6]\n",
       "7   7    b  [a7,b7, aa7,bb7, aaa7,bbb7]\n",
       "8   8    a  [a8,b8, aa8,bb8, aaa8,bbb8]\n",
       "9   9    b  [a9,b9, aa9,bb9, aaa9,bbb9]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 把不用的列删除掉\n",
    "drop_names = list(df.loc[:, \"a\":\"bbb\"].columns.values)\n",
    "df.drop(drop_names, axis=1, inplace=True)\n",
    "# 进行merge列拆分，然后explode\n",
    "df[\"merge\"] = df[\"merge\"].str.split(\"#\")\n",
    "df.explode(\"merge\")\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>a0</td>\n",
       "      <td>b0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>aa0</td>\n",
       "      <td>bb0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>a</td>\n",
       "      <td>aaa0</td>\n",
       "      <td>bbb0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>a1</td>\n",
       "      <td>b1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>aa1</td>\n",
       "      <td>bb1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>b</td>\n",
       "      <td>aaa1</td>\n",
       "      <td>bbb1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>a2</td>\n",
       "      <td>b2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>aa2</td>\n",
       "      <td>bb2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>a</td>\n",
       "      <td>aaa2</td>\n",
       "      <td>bbb2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>a3</td>\n",
       "      <td>b3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>aa3</td>\n",
       "      <td>bb3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>b</td>\n",
       "      <td>aaa3</td>\n",
       "      <td>bbb3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>a4</td>\n",
       "      <td>b4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>aa4</td>\n",
       "      <td>bb4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>a</td>\n",
       "      <td>aaa4</td>\n",
       "      <td>bbb4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>a5</td>\n",
       "      <td>b5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>aa5</td>\n",
       "      <td>bb5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>b</td>\n",
       "      <td>aaa5</td>\n",
       "      <td>bbb5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>a6</td>\n",
       "      <td>b6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>aa6</td>\n",
       "      <td>bb6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>a</td>\n",
       "      <td>aaa6</td>\n",
       "      <td>bbb6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>a7</td>\n",
       "      <td>b7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>aa7</td>\n",
       "      <td>bb7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>b</td>\n",
       "      <td>aaa7</td>\n",
       "      <td>bbb7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>a8</td>\n",
       "      <td>b8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>aa8</td>\n",
       "      <td>bb8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>8</td>\n",
       "      <td>a</td>\n",
       "      <td>aaa8</td>\n",
       "      <td>bbb8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>a9</td>\n",
       "      <td>b9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>aa9</td>\n",
       "      <td>bb9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>9</td>\n",
       "      <td>b</td>\n",
       "      <td>aaa9</td>\n",
       "      <td>bbb9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id name     a     b\n",
       "0   0    a    a0    b0\n",
       "0   0    a   aa0   bb0\n",
       "0   0    a  aaa0  bbb0\n",
       "1   1    b    a1    b1\n",
       "1   1    b   aa1   bb1\n",
       "1   1    b  aaa1  bbb1\n",
       "2   2    a    a2    b2\n",
       "2   2    a   aa2   bb2\n",
       "2   2    a  aaa2  bbb2\n",
       "3   3    b    a3    b3\n",
       "3   3    b   aa3   bb3\n",
       "3   3    b  aaa3  bbb3\n",
       "4   4    a    a4    b4\n",
       "4   4    a   aa4   bb4\n",
       "4   4    a  aaa4  bbb4\n",
       "5   5    b    a5    b5\n",
       "5   5    b   aa5   bb5\n",
       "5   5    b  aaa5  bbb5\n",
       "6   6    a    a6    b6\n",
       "6   6    a   aa6   bb6\n",
       "6   6    a  aaa6  bbb6\n",
       "7   7    b    a7    b7\n",
       "7   7    b   aa7   bb7\n",
       "7   7    b  aaa7  bbb7\n",
       "8   8    a    a8    b8\n",
       "8   8    a   aa8   bb8\n",
       "8   8    a  aaa8  bbb8\n",
       "9   9    b    a9    b9\n",
       "9   9    b   aa9   bb9\n",
       "9   9    b  aaa9  bbb9"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4. 将数据explode还原成多列\n",
    "df_explode = df.explode(\"merge\")\n",
    "df_explode[\"a\"]=df_explode[\"merge\"].str.split(\",\").str[0]\n",
    "df_explode[\"b\"]=df_explode[\"merge\"].str.split(\",\").str[1]\n",
    "df_explode.drop(\"merge\", axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
